/**
 * 
 */
package com.jm.web.form.baseitem;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.StringHelper;
import com.jm.web.data.form.Item;
import com.jm.web.form.support.FormItem;

/**
 * @author yanjie
 *
 * 2011/10/6 上午9:31:22
 */
public class RepeatListItem extends ListItem{
	public final static int TAG_Flow = 1 ;//不以表结构显示项。
	public final static int TAG_TABLE = 2 ;//在表中显示项。
	
	public final static int TAG_Horizontal = 1; //列表项以行的形式水平显示，从左到右、自上而下地加载，直到呈现出所有的项。
	public final static int TAG_Vertical = 2; //列表项以列的形式垂直显示，自上而下、从左到右地加载，直到呈现出所有的项。
	
	private int layout = TAG_Flow;
	private int repeatColumns = 1;
    private int direction = TAG_Horizontal;
    private String valueSplit = "";
    private List values = null;
    
	/**
	 * @param tag
	 */
	public RepeatListItem() {}
	
	@Override
	public void onRenderSingleBody(JspWriter writer, FormItem item) {
		try {
			//进行排版输出
	        if (this.getLayout() == TAG_TABLE) {
	            if (this.getRepeatColumns() == 0) {
	                setRepeatColumns(1);
	            }
	            
	            int nRowCount = this.getListItems().size() / getRepeatColumns();
	            
	            if (nRowCount * getRepeatColumns() < this.getListItems().size()) {
	                nRowCount += 1;
	            }
	            
	            int nCellWidth = 100 / getRepeatColumns();
	
	            //按照表格进行输出
	            writer.write(
					        "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
				
	
	            for (int nRowIndex = 0; nRowIndex < nRowCount; nRowIndex++) {
	                //输出行标记
	                writer.write("<TR>");
	                for (int nColIndex = 0; nColIndex < getRepeatColumns();
	                                     nColIndex++) {
	                    if (nRowIndex != 0 ||
	                        (nColIndex == getRepeatColumns() - 1)) {
	                        writer.write("<TD>");
	                    } else {
	                        writer.write(String.format(
	                                "<TD width=\"%1$d%%\">",
	                                nCellWidth));
	                    }
	
	                    int nItemIndex = -1;
	                    nItemIndex = nRowIndex * getRepeatColumns() +
	                                 nColIndex;
	                    if (this.getDirection() == TAG_Vertical) {
	                        //垂直输出
	                        nItemIndex = nColIndex * nRowCount + nRowIndex;
	                    }
	
	                    if (nItemIndex >= this.getListItems().size()) {
	                        //无效
	                        writer.write("&nbsp;");
	                    } else {
	                        OutputSubItem(writer,nItemIndex,this.getListItems().Get(nItemIndex));
	                    }
	                    writer.write("</TD>");
	                }
	                writer.write("</TR>");
	            }
	            writer.write("</table>");
	        } else {
	            //按照流输出
	            int nItemCount = this.getListItems().size();
	            for (int i = 0; i < nItemCount; i++) {
	            	OutputSubItem(writer,i,this.getListItems().Get(i));
	            }
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void OutputSubItem(JspWriter writer,int index,Item listItem){
		
	}
	
	@Override
    public void OnSetProperty(String strName, String strValue) {
    	if (strName.compareToIgnoreCase("layout") == 0)
		{
			this.layout = StringHelper.GetValue(strValue, this.layout);
			return;
		}
		
    	if (strName.compareToIgnoreCase("repeatColumns") == 0)
		{
			this.repeatColumns = StringHelper.GetValue(strValue, this.repeatColumns);
			return;
		}
    	
    	if (strName.compareToIgnoreCase("direction") == 0)
		{
			this.direction = StringHelper.GetValue(strValue, this.direction);
			return;
		}
    	
    	if (strName.compareToIgnoreCase("valueSplit") == 0)
		{
			this.valueSplit = strValue;
			
			if(!StringHelper.IsNullOrEmpty(this.valueSplit) && !StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("values",""))){
				String tempValues = this.getBaseTag().GetExtValue("values","");
				String[] ay =  tempValues.split("["+this.valueSplit+"]");
				if(ay.length > 0){
					if(this.values==null)this.values = new ArrayList();
					this.values.clear();
					for (int i = 0; i < ay.length; i++) {
						if(!StringHelper.IsNullOrEmpty(ay[i].trim()))
							this.values.add(ay[i].trim());
					}
				}
			}
			return;
		}
    	
    	super.OnSetProperty(strName, strValue);
    }
	
	@Override
	public void OnSetProperty(String strName, Object object) {
		if("values".equalsIgnoreCase(strName))
		{
			if(object instanceof String[]){
				String[] ay = (String[])object;
				if(ay != null && ay.length > 0){
					if(this.values==null)this.values = new ArrayList();
					this.values.clear();
					for (int i = 0; i < ay.length; i++) {
						if(!StringHelper.IsNullOrEmpty(ay[i].trim()))
							this.values.add(ay[i].trim());
					}
				}
			}else if(object instanceof List){
				this.values = (List)object;
			}
			return;
		}
		super.OnSetProperty(strName, object);
	}
	
	public int getLayout() {
		return layout;
	}

	public void setLayout(int layout) {
		this.layout = layout;
	}

	public int getRepeatColumns() {
		return repeatColumns;
	}

	public void setRepeatColumns(int repeatColumns) {
		this.repeatColumns = repeatColumns;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public List getValues() {
		return values;
	}
	
	public boolean valuesIsEmpty(){
		return this.values == null || this.values.size() == 0 ;
	}
	
}
